package com.example.demo.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.time.DateUtils;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.check.LoginCheck;
import com.example.demo.auth.constant.SessionConstants;
import com.example.demo.auth.service.AuthCheck;
import com.example.demo.domain.vo.LoginInfo;
import com.example.demo.domain.vo.LogonUser;
import com.example.demo.service.LoginService;
import com.exception.BusinessException;
import com.utils.CookieUtils;
import com.utils.StringUtils;


@Controller
@RequestMapping("/")
public class LoginController {
	/**
	 * 登录服务
	 */
	@Autowired
	private LoginService loginService;
	/**
     * 登录系统
     *
     * @param loginInfo
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/login")
    @LoginCheck(value = false)
    @AuthCheck(false)
    public void login(HttpServletRequest request, HttpServletResponse response) {
    	HttpSession session = request.getSession();
    	//判断是否已登录了
    	LogonUser user = (LogonUser) request.getSession().getAttribute(SessionConstants.USER);
    	if(user==null) {
    		//单点登录成功后，将cas的session转为本地信息。
            
            AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
            if(principal==null){
            	throw new BusinessException("未发统一登录上下文，请重新登录");
            }
            String userName = principal.getName();//获取登录用户名，实际根据东软做调整
            if(StringUtils.isBlank(userName)){
            	//用户应该不会为空，如果空表示单登录未登录完成，
            	throw new BusinessException("系统未完成单点登录 ，请重新登录！");
            }
            LoginInfo loginInfo = new LoginInfo();
            loginInfo.setLoginName(userName);
            loginInfo.setPassword("123");//
            //ContextHolder.getUserContext().put("aaa027", "410100");
            //先将用户名放入cookies中
            CookieUtils.addCookieLongTime(response, SessionConstants.LOGIN_NAME,loginInfo.getLoginName());
            /**
             * 更改为不使用缓存的实现
             */
            //LogonUser logonUser = SecurityUtils.login(loginInfo);
            LogonUser logonUser = this.loginService.login(loginInfo);
            if(logonUser!=null){
                request.getSession().setAttribute(SessionConstants.USER,logonUser);    
            }else{                     
                throw new BusinessException("重建上下文失败，请联系管理员");
            }
    	}else{
    		
    	}
        //转向用户请求的URL
    	String requrl = request.getParameter("requrl");
        if(StringUtils.isBlank(requrl)){
        	
        	throw new BusinessException("不能直接调用登录URL");
        }
        try {
			response.sendRedirect(requrl);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //return "forward:/"+requrl;
    }
}
